import axios from "axios";
import { getStore } from "@/utils/localStorage.js";
const request = ({
  method,
  url,
  params,
  data,
  headers,
  responseType,
  onUploadProgress,
  transformRequest
}) => {
  if(headers != undefined){
    headers = {
      ...headers,
      "Access-Control-Allow-Origin": "*"
    }
  }else{
    headers = {
      "Access-Control-Allow-Origin": "*"
    }
  }
  if(getStore("userinfo") && getStore("userinfo") != undefined){
    if(headers != undefined){
      headers = {
        ...headers,
        "token": getStore("userinfo").token
      }
    }else{
      headers = {
        "token": getStore("userinfo").token
      }
    }
    
  }
  
  return axios({
    method,
    url,
    params,
    data: data || {},
    headers,
    responseType,
    onUploadProgress,
    transformRequest
  });
};

const post = ({ url, params, data, headers, onUploadProgress, transformRequest }) => {
  return request({
    method: "POST",
    url,
    params,
    data,
    headers,
    onUploadProgress,
    transformRequest
  });
};

const get = ({ url, params, data, headers }) => {
  return request({
    method: "GET",
    url,
    params,
    data,
    headers
  });
};

const put = ({ url, params, data, headers }) => {
  return request({
    method: "PUT",
    url,
    params,
    data,
    headers
  });
};

const del = ({ url, params, data, headers }) => {
  return request({
    method: "DELETE",
    url,
    params,
    data,
    headers
  });
};

const submit = ({ url, params, data, headers }) => {
  return request({
    method: "POST",
    url,
    params,
    data,
    headers: {
      ...headers,
      "Content-Type": "application/x-www-form-urlencoded"
    }
  });
};

const multipartRequest = ({
  url,
  params,
  constructingBody,
  headers,
  onUploadProgress
}) => {
  const formData = new FormData();
  constructingBody && constructingBody(formData);
  return post({
    url,
    params,
    data: formData,
    headers,
    onUploadProgress
  });
};

const upload = ({
  url,
  params,
  data,
  fileObject,
  fileName,
  headers,
  onUploadProgress,
  appendDatas
}) => {
  return multipartRequest({
    url,
    params,
    constructingBody: formData => {
      console.log('data',data);
      formData.append(fileName, fileObject);
      data && formData.append("data", data);
      if (appendDatas) {
        for (let key in appendDatas) {
          let value = appendDatas[key];
          formData.append(key, value);
        }
      }
    },
    headers,
    onUploadProgress
  });
};

const download = ({ method, url, params, data }) => {
  return request({ method, url, params, data, responseType: "blob" });
};

const configure = ({ baseURL, interceptors }) => {
  axios.defaults.baseURL = baseURL;
  axios.interceptors.response.use(
    response => {
      return (
        (interceptors &&
          interceptors.response.onFulfilled &&
          interceptors.response.onFulfilled(response)) ||
        response
      );
    },
    error => {
      return (
        (interceptors &&
          interceptors.response.onRejected &&
          interceptors.response.onRejected(error)) ||
        Promise.reject(error)
      );
    }
  )

};

export default {
  configure,
  request,
  get,
  post,
  put,
  del,
  submit,
  multipartRequest,
  upload,
  download
};
